package com.log.event;


import cn.hutool.core.util.StrUtil;
import com.log.pojo.OptLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.function.Consumer;


/**
 * 异步监听日志事件
 *
 * @author zuihou
 * @Date 2019-07-01 15:13
 */
@Slf4j
@AllArgsConstructor
public class SysLogListener {

    private final Consumer<OptLog> consumer;

    @Async
    @Order
    @EventListener(SysLogEvent.class)
    public void saveSysLog(SysLogEvent event) {
        OptLog sysLog = (OptLog) event.getSource();
        // 非租户模式 (NONE) ， 需要修改这里的判断
        if (sysLog == null) {
            log.warn("日志信息为空，忽略操作日志=={}", "");
            return;
        }
        if (sysLog.getMultiTenantType() != null && sysLog.getMultiTenantType().equalsIgnoreCase("SCHEMA") && StrUtil.isEmpty(sysLog.getTenantCode())) {
            log.warn("租户编码不存在，忽略操作日志=={}", sysLog);
            return;
        }
        consumer.accept(sysLog);
    }

}
